package com.apexedu.biz.service.impl;

import com.apexedu.framework.util.DBUtil;
import com.apexedu.identity.service.impl.TreeSupport;
import com.apexedu.util.StringUtil;
import net.ysen.tree.entitybean.TreeNode;

import java.sql.SQLException;
import java.util.List;

/**
 * 树结构数据源 业务类型
 *
 * @author Linyang datetime 2011-7-20
 */
@SuppressWarnings("unchecked")
public class BizTreeSupport extends TreeSupport {
    /**
     * 取部门树数据源
     *
     * @return
     * @throws SQLException
     */
    public static List<TreeNode> queryTreeData() throws SQLException {
        StringBuilder hql = new StringBuilder("select appid as id,appname as text,(select count(*) from t_sys_menu where BELONGTOAPP=a.appid) as childCount, ");
        hql.append("replace(appurl,' ','') as value,'000' as parentid  ").append(" from t_access_app_reg a order by ordernum desc,id");
        List<TreeNode> list = DBUtil.queryAllBeanList(hql.toString(), TreeNode.class);
        TreeNode root = new TreeNode();
        root.setId("000");
        root.setParentid("");
        root.setText("资源管理中心");
        root.setChildCount(list.size());
        root.setValue("000");
        list.add(0, root);
        return list;
    }

    /**
     * 取树结构数据源
     *
     * @return
     * @throws SQLException
     */
    public static List<TreeNode> queryTreeData(String appid) throws SQLException {
        String sql = "select menuid as id,menuname||' ('||ordernum||'）' as text,(select count(*) from t_sys_menu where upmenuid=a.menuid) as childCount," +
                "upmenuid as parentid from t_sys_menu a where 1=1 ";
        if (StringUtil.isNotBlank(appid)) {
            sql += " and a.belongtoapp = '" + appid + "'";
        }
        List<TreeNode> list = DBUtil.queryAllBeanList(sql +" order by ordernum desc", TreeNode.class);
        TreeNode root = new TreeNode();
        root.setId("000");
        root.setParentid("");
        root.setText("资源管理中心");
        root.setChildCount(list.size());
        root.setValue("000");
        list.add(0, root);
        return list;
    }

    public static List<TreeNode> queryTreeData(String appid, String upMenuId) throws SQLException {
        String sql = "select menuid as id,menuname||' ('||ordernum||'）' as text,(select count(*) from t_sys_menu where upmenuid=a.menuid) as childCount," +
                "upmenuid as parentid from t_sys_menu a where 1=1 ";
        if (StringUtil.isNotBlank(appid)) {
            sql += " and a.belongtoapp = '" + appid + "'";
        }
        if(StringUtil.isNotBlank(upMenuId)){
            sql += " and a.upmenuid = '" + upMenuId + "'";
        }else{
            sql += " and a.upmenuid = '000'";
        }
        List<TreeNode> list = DBUtil.queryAllBeanList(sql +" order by ordernum desc", TreeNode.class);
        if(StringUtil.isBlank(upMenuId)){
            TreeNode root = new TreeNode();
            root.setId("000");
            root.setParentid("");
            root.setText("资源管理中心");
            root.setChildCount(list.size());
            root.setValue("000");
            list.add(0, root);
        }
        return list;
    }
}
